//
//  HomePageUserInfoHeadView.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//
#import "HomePageUserInfoHeadView.h"

@interface HomePageUserInfoHeadView ()
@property (nonatomic,strong)UIImageView *backgroundImgView;
@property (nonatomic,strong)UIView *newsCountView;
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UILabel *likeLb;
@property (nonatomic,strong)UILabel *followLb;
@property (nonatomic,strong)UILabel *fansLb;
@property (nonatomic,strong)UILabel *likeCountLb;
@property (nonatomic,strong)UILabel *followCountLb;
@property (nonatomic,strong)UILabel *fansCountLb;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *descLb;
@property (nonatomic,strong)UIButton *followBtn;


@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UILabel *dynamicLb;
@property (nonatomic,strong)UILabel *dynamicCount;

@property (nonatomic,strong)CAGradientLayer *gradientLayer;



@property (nonatomic,strong)UIView *line;


@property (nonatomic,strong)UIView *lessonView;

@property (nonatomic,strong)UIView *userInfoView;


@property (nonatomic,strong)UIView *headerBgView;

// 新增
@property (nonatomic,strong)UILabel *likedLb;
@property (nonatomic,strong)UILabel *likedCount;

@property (nonatomic,strong)UIButton *likedButton;
@property (nonatomic,strong)UIButton *dynamicButton;

@property (nonatomic,strong)UIImageView *countView;

@property (nonatomic,assign)CGPoint lastContentViewContentOffset;
@property (nonatomic,assign)NSInteger currentIndex;




@end

@implementation HomePageUserInfoHeadView


- (CAGradientLayer *)gradientLayer{
    if (!_gradientLayer) {
        _gradientLayer =  [CAGradientLayer layer];
        _gradientLayer.frame = self.followBtn.bounds;
        _gradientLayer.endPoint = CGPointMake(0, 0);
        _gradientLayer.startPoint = CGPointMake(1.0, 0);
        _gradientLayer.colors = @[(id)RGBA(247, 131, 97, 1).CGColor,(id)RGBA(245, 75, 100, 1).CGColor];
    }
    return _gradientLayer;
}

- (instancetype)init{
    if (self = [super init]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.containView = [[UIView alloc]init];
    
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self).priorityHigh();
        make.width.mas_equalTo(kScreenW);
    }];
    
    self.backgroundImgView = [[UIImageView alloc]init];
    self.backgroundImgView.image = [UIImage imageNamed:@"mine_user_bg_tag"];
    [self.containView addSubview:self.backgroundImgView];
    
    [self.backgroundImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
        make.height.mas_equalTo(96 + kNavStatusBarHeight);
    }];
  
    
    self.userInfoView = [[UIView alloc]init];
    self.userInfoView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.userInfoView];
    [self.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backgroundImgView.mas_bottom);
        make.left.right.equalTo(self.containView);
        make.height.mas_equalTo(180);
    }];
    
  
    self.headerBgView = [[UIView alloc]init];
    self.headerBgView.backgroundColor = [UIColor whiteColor];
    self.headerBgView.layer.cornerRadius = 45;
    self.headerBgView.layer.masksToBounds = YES;
    [self.containView addSubview:self.headerBgView];
    
    self.headerImgView = [[UIImageView alloc]init];
    self.headerImgView.image = [UIImage imageNamed:@"home_headIcon_tag"];
    self.headerImgView.layer.cornerRadius = 40;
    self.headerImgView.layer.masksToBounds = YES;
    
    [self.containView addSubview:self.headerImgView];
   
    [self.headerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.headerImgView);
        make.size.mas_equalTo(CGSizeMake(90, 90));
    }];
    
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.left.equalTo(self.containView).offset(20);
        make.top.equalTo(self.userInfoView.mas_top).offset(-16);
    }];
    
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = C51;
    self.nameLb.font = [UIFont boldSystemFontOfSize:18];
    [self.containView addSubview:self.nameLb];
    
    
     [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.headerImgView.mas_bottom).offset(20);
         make.left.equalTo(self.containView).offset(16);
     }];
    
    self.descLb = [[UILabel alloc]init];
    self.descLb.text = @"高考加油，高考必胜！";
    self.descLb.textColor = C51;
    self.descLb.font = font(12);
    [self.containView addSubview:self.descLb];
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(4);
        make.left.equalTo(self.headerImgView);
        make.right.equalTo(self.containView).offset(-16);
    }];

   
    self.likeCountLb = [[UILabel alloc]init];
    self.likeCountLb.textColor = C51;
    self.likeCountLb.font = [UIFont boldSystemFontOfSize:17];
    [self.userInfoView addSubview:self.likeCountLb];
    
    [self.likeCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(16);
        make.bottom.equalTo(self.userInfoView).offset(-16);
    }];
    
    self.likeLb = [[UILabel alloc]init];
    self.likeLb.text = @"获赞";
    self.likeLb.textColor = C153;
    self.likeLb.font = font(12);
    [self.userInfoView addSubview:self.likeLb];
    [self.likeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeCountLb);
        make.left.equalTo(self.likeCountLb.mas_right).offset(4);
    }];
    
    self.followCountLb = [[UILabel alloc]init];
    self.followCountLb.textColor = C51;
    self.followCountLb.font = [UIFont boldSystemFontOfSize:17];
    [self.userInfoView addSubview:self.followCountLb];
    
    [self.followCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeCountLb);
        make.left.equalTo(self.likeLb.mas_right).offset(24);
    }];
    
    self.followLb = [[UILabel alloc]init];
    self.followLb.text = @"关注";
    self.followLb.textColor = C153;
    self.followLb.font = font(12);
    [self.userInfoView addSubview:self.followLb];
    [self.followLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeLb);
        make.left.equalTo(self.followCountLb.mas_right).offset(4);
    }];
    
    
    self.fansCountLb = [[UILabel alloc]init];
    self.fansCountLb.textColor = C51;
    self.fansCountLb.font = [UIFont boldSystemFontOfSize:17];
    [self.userInfoView addSubview:self.fansCountLb];
    
    [self.fansCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeCountLb);
        make.left.equalTo(self.followLb.mas_right).offset(24);
    }];
    
    self.fansLb = [[UILabel alloc]init];
    self.fansLb.text = @"粉丝";
    self.fansLb.textColor = C153;
    self.fansLb.font = font(12);
    [self.userInfoView addSubview:self.fansLb];
    [self.fansLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeLb);
        make.left.equalTo(self.fansCountLb.mas_right).offset(4);
    }];
    
    
    self.followBtn = [[UIButton alloc]init];
    [self.followBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
    [self.followBtn addTarget:self action:@selector(followMethod) forControlEvents:UIControlEventTouchUpInside];
    self.followBtn.titleLabel.font = font(14);
    self.followBtn.layer.cornerRadius = 15;
    self.followBtn.layer.masksToBounds = YES;
    self.followBtn.hidden = NO;
    self.followBtn.backgroundColor = RGBA(224, 224, 224, 1);
    [self.userInfoView addSubview:self.followBtn];

    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userInfoView).offset(22);
        make.right.equalTo(self.userInfoView).offset(-16);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];

    self.newsCountView = [[UIView alloc]init];
    self.newsCountView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.newsCountView];
   
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userInfoView.mas_bottom);
        make.left.equalTo(self.containView).offset(16);
        make.right.equalTo(self.containView).offset(-16);
        make.height.mas_equalTo(0.5);
    }];
    
    
    [self.newsCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.containView);
        make.top.equalTo(self.line.mas_bottom).offset(16);
        make.height.mas_equalTo(40);
    }];
    
    self.countView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_segment_line_tag"]];
    [self.newsCountView addSubview:self.countView];
    
    self.likedLb = [[UILabel alloc]init];
    self.likedLb.text = @"作品";
    self.likedLb.textColor = C51;
    self.likedLb.font = [UIFont boldSystemFontOfSize:17];
    [self.newsCountView addSubview:self.likedLb];
    [self.likedLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.newsCountView).offset(35);
        make.centerY.equalTo(self.newsCountView);
    }];
    self.likedCount = [[UILabel alloc]init];
    self.likedCount.textColor = C51;
    self.likedCount.font = font(14);
    [self.newsCountView addSubview:self.likedCount];
    [self.likedCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.likedLb);
        make.left.equalTo(self.likedLb.mas_right).offset(2);
    }];

    self.dynamicLb = [[UILabel alloc]init];
    self.dynamicLb.text = @"动态";
    self.dynamicLb.textColor = C51;
    self.dynamicLb.font = [UIFont boldSystemFontOfSize:17];
    [self.newsCountView addSubview:self.dynamicLb];
    [self.dynamicLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likedLb.mas_left).offset(-70);
        make.centerY.equalTo(self.newsCountView);
    }];


    self.dynamicCount = [[UILabel alloc]init];
    self.dynamicCount.textColor = C51;
    self.dynamicCount.font = font(14);
    [self.newsCountView addSubview:self.dynamicCount];
    [self.dynamicCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dynamicLb.mas_right).offset(2);
        make.bottom.equalTo(self.dynamicLb);
    }];

   
    [self.countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.dynamicLb);
        make.width.mas_equalTo(35);
    }];

    self.likedButton = [[UIButton alloc]init];
    [self.likedButton addTarget:self action:@selector(likedMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.newsCountView addSubview:self.likedButton];

    self.dynamicButton = [[UIButton alloc]init];
    [self.dynamicButton addTarget:self action:@selector(dynamicMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.newsCountView addSubview:self.dynamicButton];

    [self.likedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.likedLb);
        make.right.equalTo(self.likedCount);
        make.top.bottom.equalTo(self.newsCountView);
    }];

    [self.dynamicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dynamicLb);
        make.right.equalTo(self.dynamicCount);
        make.top.bottom.equalTo(self.newsCountView);
    }];
//
//
    self.headerImgView.userInteractionEnabled = YES;
    
    self.nameLb.userInteractionEnabled = YES;
    
    self.descLb.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *headerIconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeUserInfoSystemMethod)];
    [self.headerImgView addGestureRecognizer:headerIconTap];
    
    UITapGestureRecognizer *descTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeUserInfoSystemMethod)];
    [self.descLb addGestureRecognizer:descTap];
    
    UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeUserInfoSystemMethod)];
    [self.nameLb addGestureRecognizer:nameTap];
    
}

- (void)likedMethod{
    [self updateLikedLineView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(getUserInfoLikedList)]) {
        [self.delegate getUserInfoLikedList];
    }
}
- (void)dynamicMethod{
    
    [self updateDynamicLineView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(getUserInfodynamicList)]) {
        [self.delegate getUserInfodynamicList];
    }
}

- (void)updateDynamicLineView{
    [self layoutIfNeeded];//如果其约束还没有生成的时候需要动画的话，就请先强制刷新后才写动画，否则所有没生成的约束会直接跑动画

    [UIView animateWithDuration:0.25 animations:^{

    [self.countView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.dynamicLb);
        make.width.mas_equalTo(35);
    }];

        [self layoutIfNeeded];//强制绘制

    }];
}
- (void)updateLikedLineView{
    [self layoutIfNeeded];//如果其约束还没有生成的时候需要动画的话，就请先强制刷新后才写动画，否则所有没生成的约束会直接跑动画

    [UIView animateWithDuration:0.25 animations:^{

    [self.countView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.bottom.equalTo(self.likedLb);
        make.width.mas_equalTo(35);
    }];

        [self layoutIfNeeded];//强制绘制

    }];
}

- (void)followMethod{
    
    BOOL status = NO;
    
    if (self.model.followType.intValue == 3 || self.model.followType.intValue == 1) {
        status = YES;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(doFollowWithStatus:)]) {
        [self.delegate doFollowWithStatus:status];
    }
}
- (void)descMethod{

}
- (void)followListMethod{
    
}
- (void)fansListMethod{
    
}

- (void)changeUserInfoSystemMethod{
    
}



- (void)updateHeaderViewAlpha:(CGFloat)alpha otherViewAlpha:(CGFloat)otherAlpha{

    self.headerImgView.alpha = alpha;
    self.userInfoView.alpha = alpha;
    self.line.alpha = alpha;
    self.descLb.alpha = alpha;
    self.nameLb.alpha = alpha;
}


- (void)updateHeaderDynamicCount:(NSInteger)itemCount{
    
    if (itemCount >= 10000) {
        NSInteger count = itemCount;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.dynamicCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.dynamicCount.text = [NSString stringWithFormat:@"%lu",itemCount];
    }
    
}
- (void)updateHeaderLikedCount:(NSInteger)itemCount{
    if (itemCount >= 10000) {
        NSInteger count = itemCount;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likedCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likedCount.text = [NSString stringWithFormat:@"%lu",itemCount];
    }
}

- (void)setModel:(HomePageUserInfoModel *)model{
    _model = model;
    
    if (model.likeCount.intValue >= 10000) {
        NSInteger count = model.likeCount.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likeCountLb.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likeCountLb.text = [NSString stringWithFormat:@"%@",model.likeCount];
    }
    
    if (model.followCount.intValue >= 10000) {
        NSInteger count = model.followCount.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.followCountLb.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.followCountLb.text = [NSString stringWithFormat:@"%@",model.followCount];
    }
    if (model.fansCount.intValue >= 10000) {
        NSInteger count = model.fansCount.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.fansCountLb.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.fansCountLb.text = [NSString stringWithFormat:@"%@",model.fansCount];
    }
    self.descLb.text = model.userSign;
    if (model.realName.length == 0) {
        NSString *string = [model.userName substringWithRange:NSMakeRange(3,4)];
        self.nameLb.text = [model.userName stringByReplacingOccurrencesOfString:string withString:@"****" options:0 range:NSMakeRange(3, 4)];
    }else{
        self.nameLb.text = model.realName;
    }

    if ([model.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        UserInfoModel *userInfo = [UserInfoModel userInfo];
        if (userInfo.imagePath.length == 0) {
            if (userInfo.sex.intValue == 0) {
                self.headerImgView.image = [UIImage imageNamed:@"defaut_header"];
            }else if (userInfo.sex.intValue == 1){
                self.headerImgView.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerImgView.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:userInfo.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }
    }else{
        if (model.imagePath.length == 0) {
            if (model.sex.intValue == 0) {
                self.headerImgView.image = [UIImage imageNamed:@"defaut_header"];
            }else if (model.sex.intValue == 1){
                self.headerImgView.image = [UIImage imageNamed:@"common_boy_tag"];
            }else{
                self.headerImgView.image = [UIImage imageNamed:@"common_girl_tag"];
            }
        }else{
            [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:model.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
        }

    }

    if ([RPAppManager getCustomerId] && [model.userId isEqualToNumber:[RPAppManager getCustomerId]]) {
        self.followBtn.hidden = YES;
    }else{
        // |0没有相互关系 1关注 2粉丝 3互相关注
        // 没有关注
        if (model.followType.intValue == 3) {
            [self.followBtn setTitle:@"互相关注" forState:UIControlStateNormal];
            [self.gradientLayer removeFromSuperlayer];
         
        }else if (model.followType.intValue == 2){
            [self.followBtn setTitle:@"+ 回关" forState:UIControlStateNormal];
            [self.followBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }else if (model.followType.intValue == 1){
            [self.followBtn setTitle:@"已关注" forState:UIControlStateNormal];
            [self.gradientLayer removeFromSuperlayer];
        }else{
            [self.followBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
            [self.followBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }
    }

    
}

- (void)setContentScrollView:(UIScrollView *)contentScrollView{
    if (_contentScrollView != nil) {
        [_contentScrollView removeObserver:self forKeyPath:@"contentOffset"];
    }
    _contentScrollView = contentScrollView;
    self.contentScrollView.scrollsToTop = NO;
    [self.contentScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"contentOffset"]) {
        CGPoint contentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
    
        if ((self.contentScrollView.isTracking || self.contentScrollView.isDecelerating)) {
           
            CGFloat ratio = contentOffset.x/self.contentScrollView.bounds.size.width;
            NSInteger baseIndex = floorf(ratio);
            CGFloat remainderRatio = ratio - baseIndex;
        
            if (remainderRatio == 0) {
                if (!(self.lastContentViewContentOffset.x == contentOffset.x && self.currentIndex == baseIndex)) {
                    if (baseIndex == 0) {
                        [self updateDynamicLineView];
                    }else{
                        [self updateLikedLineView];
                    }
                    self.currentIndex = baseIndex;
                }
            }
        }
        self.lastContentViewContentOffset = contentOffset;
    }
}

- (void)dealloc{
    if (self.contentScrollView) {
        [self.contentScrollView removeObserver:self forKeyPath:@"contentOffset"];
    }
}

@end
